<template>
  <view class="zmt-label-container">
    <!-- 选项卡标题 -->
    <view class="zmt-label-tabs">
      <view 
        v-for="(tab, index) in tabs" 
        :key="index"
        class="zmt-label-tab"
        :class="{ 'active': activeIndex === index }"
        @click="switchTab(index)"
      >
        <text class="zmt-label-tab-text">{{ tab.title }}</text>
      </view>
    </view>
    
    <!-- 选项卡内容 -->
    <view class="zmt-label-content">
      <view 
        v-for="(tab, index) in tabs" 
        :key="index"
        class="zmt-label-pane"
        :class="{ 'active': activeIndex === index }"
      >
        <!-- 富文本编辑器容器 -->
        <view :id="`aieditor-en-container-${index}`" class="aieditor-container"></view>
      </view>
    </view>
  </view>
</template>

<script>
// 导入AiEditor
import { AiEditor } from 'aieditor';
import 'aieditor/dist/style.css';

export default {
  name: 'zmt-label',
  props: {
    // 传入的内容参数，格式为数组：[{ html: '', markdown: '' }, ...]
    content: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      // 选项卡配置
      tabs: [
        { title: '参数规格', key: 'specification' },
        { title: '技术说明', key: 'technical' },
        { title: '使用说明', key: 'example' }
      ],
      // 当前激活的选项卡索引
      activeIndex: 0,
      // AiEditor实例
      aiEditors: {}
    };
  },
  watch: {
    // 监听content变化，更新编辑器内容
    content: {
      deep: true,
      handler(newVal) {
        this.updateEditorContent();
      }
    }
  },
  mounted() {
	  setTimeout(() => {
		  // 初始化富文本编辑器
		  this.initAiEditors();
	  },1000);
    
  },
  beforeDestroy() {
    // 销毁编辑器实例
    this.destroyAiEditors();
  },
  methods: {
    /**
     * 初始化所有选项卡的富文本编辑器
     */
    initAiEditors() {
		var that = this;
      this.tabs.forEach((tab, index) => {
        const containerId = `aieditor-en-container-${index}`;
        const container = document.getElementById(containerId);
        
        if (container) {
          // 创建AiEditor实例
          const aiEditor = new AiEditor({
            element: container,
            placeholder: `请输入${tab.title}...`,
            height: '250px',
			content: this.content[index].html,
            // 监听内容变化事件
            onChange: () => {
              this.emitContentChange();
            }
          });
          
          // 保存编辑器实例
          this.aiEditors[index] = aiEditor;
        }
      });
      
      // 更新编辑器内容
	  that.updateEditorContent();
    },
    
    /**
     * 更新编辑器内容
     */
    updateEditorContent() {
		console.log('内容',this.content);
      if (this.content.length === 0) return;
      
      this.tabs.forEach((tab, index) => {
        // 从数组中获取对应索引的内容
        const tabContent = this.content[index];
        if (tabContent && this.aiEditors[index]) {
          if (tabContent.markdown) {
            // 如果有markdown内容，优先使用markdown
            this.aiEditors[index].setMarkdownContent(tabContent.markdown);
          } else if (tabContent.html) {
            // 否则使用html内容
            this.aiEditors[index].setContent(tabContent.html);
          }
        }
      });
    },
    
    /**
     * 销毁所有编辑器实例
     */
    destroyAiEditors() {
      Object.values(this.aiEditors).forEach(aiEditor => {
        if (aiEditor && typeof aiEditor.destroy === 'function') {
          aiEditor.destroy();
        }
      });
      this.aiEditors = {};
    },
    
    /**
     * 切换选项卡
     * @param {number} index - 选项卡索引
     */
    switchTab(index) {
      this.activeIndex = index;
      
      // 确保切换后编辑器获得焦点
      this.$nextTick(() => {
        if (this.aiEditors[index]) {
          this.aiEditors[index].focus();
        }
      });
    },
    
    /**
     * 获取当前所有选项卡的内容
     * @returns {Object} 包含每个选项卡内容的对象
     */
    getContent() {
      const content = {};
      
      this.tabs.forEach((tab, index) => {
        if (this.aiEditors[index]) {
          content[index] = {
            html: this.aiEditors[index].getHtml(),
            markdown: this.aiEditors[index].getMarkdown(),
            key: tab.key,
            title: tab.title
          };
        }
      });
      
      return content;
    },
    
    /**
     * 触发内容变化事件
     */
    emitContentChange() {
      const content = this.getContent();
      this.$emit('content-change', content);
    }
  }
};
</script>
<style>
	.aie-content,.ProseMirror,.tiptap{height:250px !important;}
</style>
<style scoped>
.zmt-label-container {
  width: 100%;
  border-radius: 8rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}

/* 选项卡样式 */
.zmt-label-tabs {
  display: flex;
  flex-direction: row;
  border-bottom: 1px solid #f0f0f0;
  background-color: #ffffff;
}

.zmt-label-tab {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20rpx 0;
  font-size: 28rpx;
  color: #666666;
  position: relative;
  cursor: pointer;
}

.zmt-label-tab.active {
  color: #007aff;
}

.zmt-label-tab.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 4rpx;
  background-color: #007aff;
}

.zmt-label-tab-text {
  padding: 0 20rpx;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 选项卡内容样式 */
.zmt-label-content {
  width: 100%;
  background-color: #ffffff;
}

.zmt-label-pane {
  display: none;
  width: 100%;
}

.zmt-label-pane.active {
  display: block;
}

/* 富文本编辑器容器样式 */
.aieditor-container {
  width: 100%;
  min-height: 250px;
  border: 1px solid #f0f0f0;
  border-radius: 8rpx;
  overflow: hidden;
}
</style>